package com.sky.service.impl;

import com.sky.annotation.AutoFill;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetMealMapper setmealMapper;

    public void add(ShoppingCartDTO shoppingCartDTO) {
        //拿到当前线程的用户id
        Long userId = BaseContext.getCurrentId();
        //检查对应用户的购物车是否存在此商品
        ShoppingCart shoppingCart = shoppingCartMapper.selectByDishIdAndSetmealIdAndFlavor(userId,shoppingCartDTO);

        if (shoppingCart == null) {
            shoppingCart = new ShoppingCart();
            //复制属性给shoppingCart
            BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
            //设置购物车所属用户id
            shoppingCart.setUserId(userId);
            //设置购物车商品数量为1
            shoppingCart.setNumber(1);
            //设置购物车商品创建时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            //根据dishId和setmealId查询商品信息
            if (shoppingCartDTO.getDishId() != null) {
                //查询商品信息
                Dish dish = dishMapper.getDish(shoppingCart.getDishId());
                //设置购物车商品名称
                shoppingCart.setName(dish.getName());
                //设置购物车商品价格
                shoppingCart.setAmount(dish.getPrice());
                //设置购物车商品图片
                shoppingCart.setImage(dish.getImage());
            } else {
                //查询套餐信息
                Setmeal setmeal = setmealMapper.getSetmealById(shoppingCart.getSetmealId());
                //设置购物车商品名称
                shoppingCart.setName(setmeal.getName());
                //设置购物车商品价格
                shoppingCart.setAmount(setmeal.getPrice());
                //设置购物车商品图片
                shoppingCart.setImage(setmeal.getImage());
            }
            //添加购物车数据
            shoppingCartMapper.insert(shoppingCart);
        } else {
            //设置购物车的用户id
            shoppingCart.setUserId(userId);
            //购物车已存在此商品，数量加1
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            //更新购物车数据
            shoppingCartMapper.updateByPrimaryKey(shoppingCart.getId(),shoppingCart.getNumber());
        }
    }

        public List<ShoppingCart> list() {
            Long userId = BaseContext.getCurrentId();
            return shoppingCartMapper.selectByUserId(userId);

        }

    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCartDTO shoppingCartDTO = new ShoppingCartDTO();
        shoppingCartMapper.delete(userId,shoppingCartDTO);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = shoppingCartMapper.selectByDishIdAndSetmealIdAndFlavor(userId,shoppingCartDTO);
        if (shoppingCart == null) {
            return;
        }
        shoppingCart.setUserId(userId);
        shoppingCart.setNumber(shoppingCart.getNumber() - 1);
        if (shoppingCart.getNumber() <= 0) {
            shoppingCartMapper.delete(userId,shoppingCartDTO);
        } else {
            shoppingCartMapper.updateByPrimaryKey(shoppingCart.getId(),shoppingCart.getNumber());
        }


    }
}

